package com.spybdai.androidstudy;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;

public class MainActivity extends ActionBarActivity {

	private CheckBox eatBox;
	private CheckBox sleepBox;
	private CheckBox dotaBox;
	private CheckBox allBox;

	private RadioGroup sexGroup;
	private RadioButton femaleButton;
	private RadioButton maleButton;
	
	private RadioGroup marriedGroup;
	private RadioButton marriedButton;
	private RadioButton notMarriedButton;
	
	private Spinner spinner = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		eatBox = (CheckBox)findViewById(R.id.eatID);
		sleepBox = (CheckBox)findViewById(R.id.sleepID);
		dotaBox = (CheckBox)findViewById(R.id.dotaID);
		allBox = (CheckBox)findViewById(R.id.allID);
		
		BoxOnCheckedChangeListener boxListener = new BoxOnCheckedChangeListener();
		eatBox.setOnCheckedChangeListener(boxListener);
		sleepBox.setOnCheckedChangeListener(boxListener);
		dotaBox.setOnCheckedChangeListener(boxListener);
		allBox.setOnCheckedChangeListener(boxListener);
		
		sexGroup = (RadioGroup)findViewById(R.id.sexGroupID);
		femaleButton = (RadioButton)findViewById(R.id.femaleButtonId);
		maleButton = (RadioButton)findViewById(R.id.maleButtonId);
		
		marriedGroup = (RadioGroup)findViewById(R.id.marryGroupID);
		marriedButton = (RadioButton)findViewById(R.id.marriedButtonId);
		notMarriedButton = (RadioButton)findViewById(R.id.notMarriedButtonId);

		RadionGroupOnCheckedChangeListener listener = new RadionGroupOnCheckedChangeListener();
		sexGroup.setOnCheckedChangeListener(listener);
		
		ArrayAdapter<CharSequence> adaptor = ArrayAdapter.createFromResource(this, R.array.city_array,
				android.R.layout.simple_spinner_item);
		adaptor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		spinner = (Spinner)findViewById(R.id.spinnerId);
		spinner.setAdapter(adaptor);
		spinner.setPrompt("City Lists");
		
	}
	
	class RadionGroupOnCheckedChangeListener implements android.widget.RadioGroup.OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			if(checkedId == femaleButton.getId()) {
				marriedButton.setChecked(true);
			} else {
				notMarriedButton.setChecked(true);
			}
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	class BoxOnCheckedChangeListener implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			if (buttonView.getId() == R.id.eatID){
				if(isChecked){
					System.out.println("eat checked");
				} else {
					System.out.println("eat unchecked");
				}
			} else if (buttonView.getId() == R.id.sleepID){
				if(isChecked){
					System.out.println("sleep checked");
				} else {
					System.out.println("sleep unchecked");
				}
			} else if (buttonView.getId() == R.id.dotaID){
				if(isChecked){
					System.out.println("dota checked");
				} else {
					System.out.println("dota unchecked");
				}
			} else if (buttonView.getId() == R.id.allID){
				if(isChecked){
					System.out.println("all checked");
					eatBox.setChecked(true);
					sleepBox.setChecked(true);
					dotaBox.setChecked(true);
				} else {
					System.out.println("all unchecked");
					eatBox.setChecked(false);
					sleepBox.setChecked(false);
					dotaBox.setChecked(false);
				}
			}
		}
		
	}

	/*
	class BoxOnClickListerner implements OnClickListener{

		@Override
		public void onClick(View v) {
			CheckBox box = (CheckBox)v;
			if (box.getId() == R.id.eatID){
				System.out.println("eat checked");
			} else if (box.getId() == R.id.sleepID){
				System.out.println("sleep checked");
			} else if (box.getId() == R.id.dotaID){
				System.out.println("dota checked");
			} else if (box.getId() == R.id.allID){
				System.out.println("all checked");
				if(box.isChecked()){
					eatBox.setChecked(true);
					sleepBox.setChecked(true);
					dotaBox.setChecked(true);
				} else {
					eatBox.setChecked(false);
					sleepBox.setChecked(false);
					dotaBox.setChecked(false);
				}
			}
		}
	} */
}
